package cn.gzyinyuan.yy.modules.sys.controller;

import cn.gzyinyuan.yy.common.annotation.SysLog;
import cn.gzyinyuan.yy.common.utils.PageUtils;
import cn.gzyinyuan.yy.common.utils.Query;
import cn.gzyinyuan.yy.common.utils.R;
import cn.gzyinyuan.yy.common.validator.ValidatorUtils;
import cn.gzyinyuan.yy.modules.sys.entity.SysConfigEntity;
import cn.gzyinyuan.yy.modules.sys.entity.SysDept;
import cn.gzyinyuan.yy.modules.sys.service.SysConfigService;
import cn.gzyinyuan.yy.modules.sys.service.SysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门
 * Created by DT人 on 2017/9/7 14:30.
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {




    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 所有配置列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:config:list")
    public R list(@RequestParam Map<String, Object> params){
        if (params.get("page")==null||params.size()==0||params.get("limit")==null){
            params = new HashMap<String, Object>();
            params.put("page",1);
            params.put("limit",10);
        }
        //查询列表数据
        Query query = new Query(params);

        List<SysDept> configList = sysDeptService.queryList(query);
        int total = sysDeptService.queryTotal(query);
        System.out.println(configList);
        PageUtils pageUtil = new PageUtils(configList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 配置信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:config:info")
    public R info(@PathVariable("id") Long id){
        SysDept config = sysDeptService.queryObject(id);

        return R.ok().put("dept", config);
    }

    /**
     * 保存配置
     */
    @SysLog("保存配置")
    @RequestMapping("/save")
    @RequiresPermissions("sys:config:save")
    public R save(@RequestBody SysDept dept){
        ValidatorUtils.validateEntity(dept);
        dept.setParentId(0);
        sysDeptService.save(dept);

        return R.ok();
    }

    /**
     * 修改配置
     */
    @SysLog("修改配置")
    @RequestMapping("/update")
    @RequiresPermissions("sys:config:update")
    public R update(@RequestBody SysDept dept){
        System.out.println(dept);
        sysDeptService.update(dept);

        return R.ok();
    }

    /**
     * 删除配置
     */
    @SysLog("删除配置")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:config:delete")
    public R delete(@RequestBody Long[] ids){
        sysDeptService.deleteBatch(ids);
        return R.ok();
    }
}
